package com.goomba.batch;


import com.goomba.demo.contants.Commons;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;

/**
 * 批量发送信息。一次可以发送的消息不超过4MB
 * 批量发送消息。不代表接收的时候也是批量的。接收的时候依旧是一条一条的数据。具体怎么控制是消费端的事情
 */
public class Producer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("batch_group");
        producer.setNamesrvAddr(Commons.nameSrvAddr);

        producer.setSendMsgTimeout(1500);

        List<Message> list = new LinkedList<>();

        producer.start();

        list.add(new Message("batch_topic", "TAG1", "order001", ("hello order 0").getBytes(StandardCharsets.UTF_8)));
        list.add(new Message("batch_topic", "TAG1", "order002", ("hello order 1").getBytes(StandardCharsets.UTF_8)));
        list.add(new Message("batch_topic", "TAG1", "order003", ("hello order 2").getBytes(StandardCharsets.UTF_8)));

        SendResult result = producer.send(list);

        System.out.println("放回的数据是" + result.toString());

        producer.shutdown();

    }
}
